package com.airnewslinkosky.ticket.ui;

import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.pay.util.BaseHelper;
import com.airnewslinkosky.ticket.sql.FavoriteInfoDB;
import com.airnewslinkosky.ticket.util.AddrUtil;
import com.airnewslinkosky.ticket.util.Constants;
import com.mobclick.android.MobclickAgent;

/**
 * 系统设置
 * @author 
 */
public class SystemSet extends BaseActivity {
	
	public final int CITY_LIST = 1;
	public final int FLYCOM_LIST = 2;
	public final int NORESULT = 3;
	public final int NOORDER = 4;
	public final int NOORDERSTAT = 5;
	public final int NET_ERROR=6;
	public final int SEARCH_ERROR=7;
	public  final int TIME_DIALOG_ID = 8;
	public  final int DIALOG_SINGLE_CHOICE = 9;
	public  final int DIALOG_LIST = 10;
	
	
	private TextView comCity,toCity;
	//出发城市,到达城市
	private LinearLayout fromCityLay,toCityLay;
	
	private Button saveBut,cleanBut;
	
	private ProgressDialog progressDialog = null;
	public Handler myHandler;
	
	AddrUtil addrUtil;
	
    private SharedPreferences sp;
	private String loginNameRe = "";
	private String isLoninOK = null;//是否登录
	private String autoLogin = null;//记着登录
	private Button backBut;
	private Toast toast;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.system_set);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
		activityList.add(this); 
		
		addrUtil = new AddrUtil(this);
		
//		sp = getSharedPreferences("UserInfoB2C", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
//		initConfig();
		
		fromCityLay = (LinearLayout)findViewById(R.id.fromCityLay);
		toCityLay = (LinearLayout)findViewById(R.id.toCityLay);
		
		comCity = (TextView)findViewById(R.id.comCity);
		toCity = (TextView)findViewById(R.id.toCity);
		
		comCity.setText(FavoriteInfoDB.selectFlySetInfoValue(this, Constants.SYSTEM_TYPE_COM_CITY));
		toCity.setText(FavoriteInfoDB.selectFlySetInfoValue(this, Constants.SYSTEM_TYPE_TO_CITY));
		
		saveBut = (Button)findViewById(R.id.saveBut);
		cleanBut = (Button)findViewById(R.id.cleanBut);
		
		OnClickListener b_ocl= new OnClickListener() { 
	    	public void onClick(View v) { 
	    		switch (v.getId()) {
	    		case R.id.fromCityLay:
	    			Intent intent = new Intent();
	    			intent.setClass(SystemSet.this, CityList.class);
	    			intent.putExtra("comcity", toCity.getText().toString());
	    			startActivityForResult(intent, R.id.comCity);
	    			break;
	    		case R.id.toCityLay:
	    			Intent intentTo = new Intent();
	    			intentTo.setClass(SystemSet.this, CityList.class);
	    			intentTo.putExtra("comcity", comCity.getText().toString());
	    			startActivityForResult(intentTo, R.id.toCity);
	    			break;
	    		case R.id.saveBut:
	    			save();
	    			break;
	    		case R.id.cleanBut:
	    			clean();
	    			break;
	    		case R.id.backBut:
	    			Intent intentR = new Intent();
	    			intentR.setClass(SystemSet.this, PersonCenter.class);
//	    			Bundle bundle=new  Bundle();
//	    			bundle.putString("loginName", loginNameRe);
//	    			bundle.putString("isLoninOK", isLoninOK);
//	    			bundle.putString("autoLogin", autoLogin);
//	    			intentR.putExtras(bundle);
	    			setResult(RESULT_OK, intentR);
	    			finish();
					break;
	    		}
	    	}
		};
		
		backBut = (Button) findViewById(R.id.backBut);
		backBut.setOnClickListener(b_ocl);
		
		fromCityLay.setOnClickListener(b_ocl);
		toCityLay.setOnClickListener(b_ocl);
		saveBut.setOnClickListener(b_ocl);
		cleanBut.setOnClickListener(b_ocl);
		
		myHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case NORESULT:
					toast = Toast.makeText(SystemSet.this, "未找到符合条件的航班", Toast.LENGTH_LONG);
    				toast.setGravity(Gravity.CENTER, 0, 0);
    				toast.show();
					break;
				case NOORDER:
					toast = Toast.makeText(SystemSet.this, "无订单信息", Toast.LENGTH_LONG);
    				toast.setGravity(Gravity.CENTER, 0, 0);
    				toast.show();
					break;
				case NOORDERSTAT:
					toast = Toast.makeText(SystemSet.this, "订单信息未获得更新", Toast.LENGTH_LONG);
    				toast.setGravity(Gravity.CENTER, 0, 0);
    				toast.show();
					break;
				case NET_ERROR:
					toast = Toast.makeText(SystemSet.this, "网络超时，连接失败", Toast.LENGTH_LONG);
    				toast.setGravity(Gravity.CENTER, 0, 0);
    				toast.show();
					break;
				case SEARCH_ERROR:
					toast = Toast.makeText(SystemSet.this, (String)msg.obj, Toast.LENGTH_LONG);
    				toast.setGravity(Gravity.CENTER, 0, 0);
    				toast.show();
					break;
				}
				super.handleMessage(msg);
			}
		};
		
	}
	
    //初始化配置
    private void initConfig() {
//    	sp = getSharedPreferences("UserInfoB2B", 0);
//        loginNameRe = sp.getString("loginName", "");
//        isLoninOK = sp.getString("isLoninOK", null);
//        autoLogin = sp.getString("isAutoLogin", null);
    }
    

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			TextView tv = (TextView) findViewById(requestCode);
			Bundle b=data.getExtras();  
			tv.setText(b.getString("city"));
			tv.requestFocus();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void save() {
		final String fcity = comCity.getText().toString();
		final String tcity = toCity.getText().toString();
		if (fcity.equals("")) {
//			Toast.makeText(this, "请选择完整信息", Toast.LENGTH_LONG).show();
			BaseHelper.showDialog(SystemSet.this, "提示", "请填写完整信息",
					R.drawable.infoicon);
			return;
		}
		
		ContentValues values = new ContentValues();
		values.put("systemType", Constants.SYSTEM_TYPE_COM_CITY);
		values.put("systemName", fcity);
		
		String comCityValue = FavoriteInfoDB.selectFlySetInfoValue(this, Constants.SYSTEM_TYPE_COM_CITY);
		if (comCityValue!=null && comCityValue.length()>0) {
			values.put("systemType", comCityValue);
			FavoriteInfoDB.updateFlySetInfo(SystemSet.this, values);
			Toast.makeText(this, "已经成功更新！", Toast.LENGTH_LONG).show();
		} else {
			FavoriteInfoDB.insertFlySetInfo(SystemSet.this, values);
			Toast.makeText(this, "已经成功增加常用航班！", Toast.LENGTH_LONG).show();
		}
		
		if(tcity!=null && tcity.length()>0) {
			values = new ContentValues();
			values.put("systemType", Constants.SYSTEM_TYPE_TO_CITY);
			values.put("systemName", tcity);
			
			String toCityValue = FavoriteInfoDB.selectFlySetInfoValue(this, Constants.SYSTEM_TYPE_TO_CITY);
			if (toCityValue!=null && toCityValue.length()>0) {
				values.put("systemType", toCityValue);
				FavoriteInfoDB.updateFlySetInfo(SystemSet.this, values);
			} else {
				FavoriteInfoDB.insertFlySetInfo(SystemSet.this, values);
			}
		}
		
	}
	
	private void clean() {
		FavoriteInfoDB.deleteFlySetInfoValue(this, Constants.SYSTEM_TYPE_COM_CITY);
		FavoriteInfoDB.deleteFlySetInfoValue(this, Constants.SYSTEM_TYPE_TO_CITY);
		comCity.setText("");
		toCity.setText("");
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  

}